<script setup>
	import {
		onLoad,
		onReachBottom,
		onPullDownRefresh
	} from "@dcloudio/uni-app";
	import {
		ref
	} from "vue";
	import nuni from '../../static/utils/request.js'
	// 请求参数对象预定义
	const queryObj = ref({
		// 查询关键词
		query: '',
		// 商品分类Id
		cid: '',
		// 页码值
		pagenum: 1,
		// 每页显示多少条数据
		pagesize: 10
	})
	onLoad(options => {
		queryObj.value.query = options.query || ''
		queryObj.value.cid = options.cid || ''
		getGoodsList()
	})
	// 需要渲染的相关数据
	const goodsList = ref([])
	const total = ref(0)
	// 请求相关数据
	const getGoodsList = async (fn) => {
		isLoading = true
		const {
			data: res
		} = await nuni.request({
			url: '/api/public/v1/goods/search',
			data: {
				...queryObj.value
			}
		})
		if (res.meta.status != 200) return nuni.$showMsg()
		goodsList.value = [...goodsList.value, ...res.message.goods]
		total.value = res.message.total
		console.log("当前列表共" + total.value + "条数据");
		fn && fn()
		isLoading = false
	}
	// 处理上划加载更多功能
	// ----节流阀
	let isLoading = false
	onReachBottom(() => {
		if (isLoading) return
		// 加载完毕？
		if (queryObj.value.pagenum * queryObj.value.pagesize >= total.value) return nuni.$showMsg("没有更多数据了")
		queryObj.value.pagenum++
		getGoodsList()
	})
	// 处理下拉刷新
	onPullDownRefresh(() => {
		queryObj.value.pagenum = 1,
			total.value = 0
		isLoading = false
		goodsList.value = []
		getGoodsList(() => uni.stopPullDownRefresh())
	})
	// 处理点击进入详情页功能
	const gotoDetail = (ele) => {
		uni.navigateTo({
			url: '/subpkg/goods_detail/goods_detail?goods_id=' + ele.goods_id
		})
	}
</script>


<template>
	<view class="goods-list">
		<view v-for="(e1, i1) in goodsList" :key="i1" @click="gotoDetail(e1)">
			<goods-item :goods="e1"></goods-item>
		</view>
	</view>
</template>

<style lang="scss">

</style>